2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
9 namespace SuperPolarity
15 public List<Actor> Children;
18 protected Texture2D Texture;
19 protected Vector2 Origin;
22 // Physical Properties
23 public Vector2 Position;
24 protected Vector2 Velocity;
25 protected Vector2 Acceleration;
28 // Constraints / Behavior
29 protected float MaxVelocity;
30 protected float AccelerationRate;
34 get { return Texture.Width; }
39 get { return Texture.Height; }
42 public Actor(Game newGame)
47 public virtual void Initialize(Texture2D texture, Vector2 position)
53 Children = new List<Actor>();
55 Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
56 Velocity = new Vector2(0, 0);
57 Acceleration = new Vector2(0, 0);
60 AccelerationRate = 10;
63 public void AutoDeccelerate(GameTime gameTime)
65 if (Acceleration.X == 0 && Velocity.X > 0)
67 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
74 Acceleration.X = -AccelerationRate;
78 if (Acceleration.X == 0 && Velocity.X < 0)
80 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
87 Acceleration.X = AccelerationRate;
91 if (Acceleration.Y == 0 && Velocity.Y > 0)
93 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
100 Acceleration.Y = -AccelerationRate;
104 if (Acceleration.Y == 0 && Velocity.Y < 0)
106 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
113 Acceleration.Y = AccelerationRate;
118 public virtual void Update(GameTime gameTime)
125 public virtual void Move(GameTime gameTime)
127 AutoDeccelerate(gameTime);
129 var maxVelocity = MaxVelocity;
131 Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
132 Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
134 if (Velocity.X > MaxVelocity)
136 Velocity.X = MaxVelocity;
139 if (Velocity.X < -MaxVelocity)
141 Velocity.X = -MaxVelocity;
144 if (Velocity.Y > MaxVelocity)
146 Velocity.Y = MaxVelocity;
149 if (Velocity.Y < -MaxVelocity)
151 Velocity.Y = -MaxVelocity;
154 Position.X = Position.X + Velocity.X;
155 Position.Y = Position.Y + Velocity.Y;
158 public void ChangeAngle()
160 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
163 public virtual void Draw(SpriteBatch spriteBatch)
165 foreach (Actor child in Children)
167 child.Draw(spriteBatch);
170 spriteBatch.Draw(Texture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f);
175 for (var i = Children.Count; i > 0; i--)
177 var actor = Children[i - 1];
178 if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds ||
179 actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds ||
180 actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds)
182 Children.Remove(actor);